package com.molyfun.parents.activity.store.view;

import android.content.Context;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class ProductTitleBar extends LinearLayout implements View.OnClickListener{

    private int mChooseTab = ProductDetailChooseTab.BAR_INFO;

    public static interface ProductDetailChooseTab{
        public static final int BAR_INFO = 1001;
        public static final int BAR_SERVICE_CONTEXT = 1002;
    }

    protected LayoutInflater layoutInflater;

    private Handler handler = new Handler();
    private ProductDetailTabClickListener listener;
    private Context context;
    private boolean inited;

    @InjectView(R.id.product_title_info)
    TextView mInfo;
    @InjectView(R.id.product_service_content)
    TextView mServiceContent;


    public ProductTitleBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ProductTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ProductTitleBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_product_title_choose_bar, this);
        ButterKnife.inject(this);
        mInfo.setOnClickListener(this);
        mServiceContent.setOnClickListener(this);
    }

    public int getCurrentChooseTab(){
        return mChooseTab;
    }

    /**
     * init view
     *
     */
    public void init() {
        if(inited){
            return;
        }

        inited = true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.product_title_info:
                if(mChooseTab != ProductDetailChooseTab.BAR_INFO){
                    if(listener != null){
                        listener.onTitleBarClick(ProductDetailChooseTab.BAR_INFO);
                    }
                    setTabClick(ProductDetailChooseTab.BAR_INFO);
                }
                break;
            case R.id.product_service_content:
                if(mChooseTab != ProductDetailChooseTab.BAR_SERVICE_CONTEXT){
                    if(listener != null){
                        listener.onTitleBarClick(ProductDetailChooseTab.BAR_SERVICE_CONTEXT);
                    }
                    setTabClick(ProductDetailChooseTab.BAR_SERVICE_CONTEXT);
                }
                break;
        }
    }

    /**
     * only view
     * @param tab
     */
    public void setTabClick(int tab){
        if(tab == ProductDetailChooseTab.BAR_INFO){
            mInfo.setBackgroundResource(R.drawable.bg_rounded_base);
            mInfo.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mServiceContent.setBackgroundColor(ContextCompat.getColor(context, R.color.color_transparent));
            mServiceContent.setTextColor(ContextCompat.getColor(context, R.color.product_title_bar_text_unchecked_color));
            mChooseTab = ProductDetailChooseTab.BAR_INFO;
        }else{
            mServiceContent.setBackgroundResource(R.drawable.bg_rounded_base);
            mServiceContent.setTextColor(ContextCompat.getColor(context, R.color.color_white));
            mInfo.setBackgroundColor(ContextCompat.getColor(context, R.color.color_transparent));
            mInfo.setTextColor(ContextCompat.getColor(context, R.color.product_title_bar_text_unchecked_color));
            mChooseTab = ProductDetailChooseTab.BAR_SERVICE_CONTEXT;
        }
    }

    public void setProductDetailTabClickListener(ProductDetailTabClickListener listener) {
        this.listener = listener;
    }

    public static interface ProductDetailTabClickListener {
        void onTitleBarClick(int chooseTab);
    }

}